/**
 * 飞机:
 */
class Plane extends Flyrole {
    //多发子弹时的子弹水平间距
    private static bullet_dis: number = 20;
    //各类飞机的参数数组
    private static hp_arr: number[] = [5, 1, 2, 6];
    private static speed_arr: number[] = [0, 3, 2, 1];
    private static radius_arr: number[] = [36, 18, 34, 84];
    private static shootType_arr: number[] = [1, 0, 1, 2];
    private static shootInterval_arr: number[] = [1000, 0, 2000, 5000];
    //子弹数，针对thootType!=0
    private bullet_count: number = 1;
    //血条
    private hp_line: Laya.ProgressBar;
    //0：不射击；1：一颗直线子弹；2：6方向发射子弹
    private shootType: number;
    //0：主角；1,2,3：小，中，大型敌机
    private type: number = 0;
    //子弹间隔
    private shootInterval: number;
    //下次子弹发射时间
    public shootTime: number;
    //子弹的buff增益有效时间：应该在Props类，简化
    public bullet_buff_time: number = 0;

    constructor(_type?: number) {
        super();
        if(!this.hp_line) {
            this.hp_line = new Laya.ProgressBar("menu/progress.png");
            this.hp_line.height = 8;
            this.addChild(this.hp_line);
        }
        if(_type != undefined){
            this.init(_type);
        }
    }

    public init(_type):Plane {
        super._init_();
        this.type = _type;
        this.hp = Plane.hp_arr[_type];
        this.speed = Plane.speed_arr[_type];
        this.bullet_count = 1;
        this.radius = Plane.radius_arr[_type];
        this.shootType = Plane.shootType_arr[_type];
        this.shootInterval = Plane.shootInterval_arr[_type];
        this.shootTime = Laya.Browser.now() + 1000; //不重新赋值可能导致连续发射子弹
        this.camp = _type == 0 ? 0 : 1;
        
        if(GameMain.game_level > 20) {
            //根据游戏等级增加子弹数量
            if(this.shootType == 0) {
                this.shootType = 1;
                this.shootInterval = 1000;
                this.bullet_count = 1;
            }else{
                this.bullet_count = 2;
            }
        }
        //根据游戏等级增加移动速度和子弹
        if(this.speed) {
            this.speed += Math.min(GameMain.game_level > 20 ? GameMain.game_level - 20 : GameMain.game_level, 16) * 0.05;
            if(this.shootInterval) {
                this.shootInterval -= (GameMain.game_level > 20 ? GameMain.game_level - 20 : GameMain.game_level) * 50;
            }
        }
        //设置血量小时
        if(this.type == 0) {
            if(GameMain.game_panel)
                GameMain.game_panel.setHeroHp(this.hp);
        }
        //重置血条
        if(this.type !== 0) {
            let t_hp_line_width = this.hp * 30;
            this.hp_line.width = t_hp_line_width;
            this.hp_line.value = 1;
            this.hp_line.x = - t_hp_line_width / 2;
            this.hp_line.y = - this.radius * 1.7;
        }else{
            this.hp_line.width = 0;
        }

        this.playAciton('fly');
        this.body.on(Laya.Event.COMPLETE, this, this.onPlayComplete);
        return this;    //链式调用
    }

    //动画结束时处理
    onPlayComplete():void {
        //击中动画播放完后恢复飞行动画
        if(this.action_now == 'hit') {
            this.playAciton('fly');
            this.is_exixt = true;
        }
        //击毁动画后结束游戏或回收
        if(this.action_now == 'down') {
            if(this.type === 0) {
                //游戏结束
                GameMain.game_panel.endGame();
                Laya.SoundManager.playSound('sound/game_over.mp3');
                this.rollbackSelf();
            }else{
                if(this.type == 3) {
                    GameMain.role_contain.addChild(Laya.Pool.getItemByClass("props", Props).init().pos(this.x, this.y))
                }
                this.rollbackSelf();
            }
        }
    }

    //回收自己
    protected rollbackSelf():void {
        super.rollbackSelf();
        this.removeSelf();
        Laya.Pool.recover('plane', this);
    }

    //飞机的射击动作
    public shoot():void {
        if(this.shootType != 0) {
            this.shootTime += this.shootInterval;
            //大飞机的八向子弹
            if(this.type == 3) {
                let bullet_num = 0;
                while(bullet_num < 8) {
                    GameMain.role_contain.addChild(Laya.Pool.getItemByClass("bullet", Bullet).init(2, bullet_num * 45).pos(this.x, this.radius / 2 + this.y));
                    bullet_num ++;
                }
            }else{
                if(this.type == 0) {
                    Laya.SoundManager.playSound('sound/bullet.mp3');
                }
                let t_bullet_type:number = this.type == 0 ? 0 : 1;
                //buff增益时间限制
                if(this.bullet_buff_time && Laya.Browser.now() >= this.bullet_buff_time) {
                    this.bullet_count = 1;
                    this.bullet_buff_time = 0;
                }
                //兼容多发子弹
                let t_bullet_count:number = this.bullet_count;
                let t_bullet_dis: number = Plane.bullet_dis;
                let t_start_pos:number = - (t_bullet_count - 1) / 2 * t_bullet_dis;
                while(t_bullet_count > 0) {
                    GameMain.role_contain.addChild(Laya.Pool.getItemByClass("bullet", Bullet).init(t_bullet_type).pos(this.x + t_start_pos, (t_bullet_type ? 1 : -1) * this.radius / 2 + this.y));
                    t_start_pos += t_bullet_dis;
                    t_bullet_count --;
                }
            }
        }
    }

    //通过动画名播放动画
    public playAciton(_action: string):void {
        super.playAciton('plane'+this.type, _action);
    }

    //被攻击
    public attacked():void {
        super.attacked();
        
        if(this.type === 0) {
            GameMain.game_panel.setHeroHp(this.hp);
        }
        if(this.hp <= 0) {
            this.playAciton('down');
            if(this.type !== 0) {
                Laya.SoundManager.playSound('sound/enemy'+this.type+'_down.mp3');
                GameMain.total_score += Plane.hp_arr[this.type];
                if(GameMain.total_score % 50 == 0) {
                    GameMain.addGameLavel();
                }
                GameMain.game_panel.setHeroScore(GameMain.total_score);
            }
        }else{
            if(this.type !== 0 && this.type !== 1){
                this.hp_line.value = this.hp / Plane.hp_arr[this.type];
                this.playAciton('hit');
            }else if(this.type === 0) {
                this.is_exixt = true;   //主角没有击中动画，所以直接充值is_exixt，其它角色在动画结束后重置
            }
        }
    }

    //获得道具
    public getBuff(_props_type: number): void {
        //目前只有主角能获得道具
        if(this.type == 0) {
            Laya.SoundManager.playSound('sound/achievement.mp3');
            
            //加子弹
            if(_props_type == 1 && this.bullet_count < 4){
                this.bullet_buff_time = Laya.Browser.now() + 18000;
                this.bullet_count ++;
            }else {
                //加血包
                if(_props_type == 0 && this.hp < 10) {
                    this.hp ++;
                    GameMain.game_panel.setHeroHp(this.hp);
                }
            }
        }
    }
}